package com.lxs.task001;

import java.util.Timer;
import java.util.TimerTask;

public class TimerExample {
    public static void main(String[] args) {
        Timer timer = new Timer();

        // 创建定时任务
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                System.out.println("定时任务执行: " + new java.util.Date());
            }
        };

        // 延迟3秒后开始执行，每隔2秒执行一次
        int delay = 3 * 1000;
        int period = 2 * 1000;
        timer.schedule(task, delay, period);

        // 10秒后取消任务
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        timer.cancel();
    }
}
